package com.retail.booking.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.retail.booking.entity.po.BookingShareRecordPo;
import com.retail.booking.entity.request.BookingShareRecordRequest;
import com.retail.booking.entity.vo.BookingShareRecordVo;
import com.retail.booking.mapper.BookingShareRecordMapper;
import com.retail.booking.service.BookingShareRecordService;
import com.retail.core.entity.ParamReq;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 海报分享记录 -每分享一次，生成一次记录 Service impl
 *
 * @author Administrator
 */
@Service
public class BookingShareRecordServiceImpl implements BookingShareRecordService {

    @Resource
    private BookingShareRecordMapper bookingShareRecordMapper;

    @Override
    public BookingShareRecordVo getModel(Long id) {
        BookingShareRecordVo bookingShareRecordVo = new BookingShareRecordVo();
        BookingShareRecordPo bookingShareRecordPo = bookingShareRecordMapper.getModel(id);
        BeanUtils.copyProperties(bookingShareRecordPo, bookingShareRecordVo);
        return bookingShareRecordVo;
    }

    @Transactional
    @Override
    public Long save(BookingShareRecordRequest request) {
        return bookingShareRecordMapper.save(request);
    }

    @Transactional
    @Override
    public Long update(BookingShareRecordRequest request) {
        bookingShareRecordMapper.update(request);
        return request.getId();
    }

    @Override
    public List<BookingShareRecordVo> listPage(ParamReq<BookingShareRecordRequest> params) {
        List<BookingShareRecordPo> pos = bookingShareRecordMapper.listPage(params);
        List<BookingShareRecordVo> res = CollUtil.newArrayList();
        for (BookingShareRecordPo bookingShareRecordPo : pos) {
            BookingShareRecordVo bookingShareRecordVo = new BookingShareRecordVo();
            BeanUtils.copyProperties(bookingShareRecordPo, bookingShareRecordVo);
            res.add(bookingShareRecordVo);
        }
        return res;
    }

    @Override
    public Integer countPage(ParamReq<BookingShareRecordRequest> params) {
        return bookingShareRecordMapper.countPage(params);
    }


    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return bookingShareRecordMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public BookingShareRecordVo getLast1ShareRecord(Long userId, Long posterId) {
        BookingShareRecordPo bookingShareRecordPo =  bookingShareRecordMapper.getLast1ShareRecord(userId, posterId, StatusFlagEnum.USE.getValue(), DelFlagEnum.NO.getValue());
        if (ObjectUtils.isEmpty(bookingShareRecordPo)) {
            return null;
        }
        BookingShareRecordVo bookingShareRecordVo = new BookingShareRecordVo();
        BeanUtils.copyProperties(bookingShareRecordPo, bookingShareRecordVo);
        bookingShareRecordVo.setStatusFlagName(StatusFlagEnum.getStatusValues(bookingShareRecordVo.getStatusFlag()));
        return bookingShareRecordVo;
    }
}